"""
连接数据库添加数据
"""
from pymysql import Connection

conn = None
cursor = None
try:
    # 1、创建连接数据库对象
    conn = Connection(
        user="root",
        password="123456",
        database="mydb1",
        host="localhost",
        port=3306,
        charset="utf8"
    )
    # 2、创建游标对象
    cursor = conn.cursor()
    # 3、通过游标对象执行sql
    sql = '''
        INSERT INTO hero(h_name,age,gender,address) VALUES
        ("张三",23,"男","河北省石家庄市"),
        ("李四",22,"男","河北省辛集市"),
        ("王五",25,"男","河北省沧州市"),
        ("赵六",18,"女","河北省保定市");
    '''
    cursor.execute(sql)
    # 4、将数据提交到数据库或者获取数据
    conn.commit()
except Exception as e:
    print(e)
finally:
#5、关闭游标对象
    if cursor:
        cursor.close()
#6、关闭数据库连接对象
    if conn:
        conn.close()